﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Algobox.JuicyLib.Apps.EODData.Files;
using Algobox.JuicyLib.MarketData.Structure;

namespace Algobox.JuicyLib.Apps.EODData
{
    public class EODDataClient
    {
        public EODDataClient(string username, string password)
        {
            m_Client = new WebClient();
            m_Client.Credentials = new NetworkCredential(username, password);

            m_ExchangeHandler = new EODDataExchanges(this);
        }
        
        WebClient m_Client;
        EODDataExchanges m_ExchangeHandler;

        public string LoadFile(string fileLocation)
        {
            try
            {
                return new ASCIIEncoding().GetString(m_Client.DownloadData(fileLocation));
            }
            catch
            {                
                try
                {
                    fileLocation = fileLocation.Insert(fileLocation.LastIndexOf('/'), "/History");
                    return new ASCIIEncoding().GetString(m_Client.DownloadData(fileLocation));
                }
                catch
                {
                    return string.Empty;
                }
            }
        }

        public bool TryGetEndOfDay(Exchanges exchange, DateTime date, out Dictionary<string, OHLC> data)
        {
            data = null;
            switch (exchange)
            {
                case Exchanges.Amex:
                    data = m_ExchangeHandler.GetAmexEndOfDay(date);
                    break;
                case Exchanges.NYSE:
                    data = m_ExchangeHandler.GetNyseEndOfDay(date);
                    break;
                case Exchanges.Nasdaq:
                    data = m_ExchangeHandler.GetNasdaqEndOfDay(date);
                    break;
                case Exchanges.Forex:
                    data = m_ExchangeHandler.GetForexEndOfDay(date);
                    break;
                case Exchanges.Opra:
                    data = m_ExchangeHandler.GetOpraEndOfDay(date);
                    break;
                case Exchanges.Milan:
                    data = m_ExchangeHandler.GetMilanEndOfDay(date);
                    break;
                case Exchanges.LSE:
                    data = m_ExchangeHandler.GetLSEEndOfDay(date);
                    break;
                case Exchanges.XTSE:
                    data = m_ExchangeHandler.GetTSXEndOfDay(date);
                    break;
                case Exchanges.XTSX:
                    data = m_ExchangeHandler.GetVentureEndOfDay(date);
                    break;
            }


            if (data != null)
                return true;

            data = new Dictionary<string, OHLC>();
            return false;
        }


        public bool TryGetSplits(Exchanges exchange, out Dictionary<string, Split> data)
        {
            string file;
            data = null;
            switch (exchange)
            {
                case Exchanges.Amex:
                    file = LoadFile(Splits.FILENAME_AMEX);
                    if (!String.IsNullOrEmpty(file))
                        data = new Splits(ref file);
                    break;
                case Exchanges.NYSE:
                    file = LoadFile(Splits.FILENAME_NYSE);
                    if (!String.IsNullOrEmpty(file))
                        data = new Splits(ref file);
                    break;
                case Exchanges.Nasdaq:
                    file = LoadFile(Splits.FILENAME_NASDAQ);
                    if (!String.IsNullOrEmpty(file))
                        data = new Splits(ref file);
                    break;
                case Exchanges.Opra:
                    file = LoadFile(Splits.FILENAME_OPRA);
                    if (!String.IsNullOrEmpty(file))
                        data = new Splits(ref file);
                    break;
                case Exchanges.Milan:
                    file = LoadFile(Splits.FILENAME_MLSE);
                    if (!String.IsNullOrEmpty(file))
                        data = new Splits(ref file);
                    break;
                case Exchanges.LSE:
                    file = LoadFile(Splits.FILENAME_LSE);
                    if (!String.IsNullOrEmpty(file))
                        data = new Splits(ref file);
                    break;
                case Exchanges.XTSE:
                    file = LoadFile(Splits.FILENAME_TSX);
                    if (!String.IsNullOrEmpty(file))
                        data = new Splits(ref file);
                    break;
                case Exchanges.XTSX:
                    file = LoadFile(Splits.FILENAME_TSXV);
                    if (!String.IsNullOrEmpty(file))
                        data = new Splits(ref file);
                    break;
            }


            if (data != null)
                return true;

            data = new Dictionary<string, Split>();
            return false;
        }
        
        public bool TryGetNames(Exchanges exchange, out Dictionary<string, string> data)
        {
            data = null;
            switch (exchange)
            {
                case Exchanges.Amex:
                    data = m_ExchangeHandler.GetAmexNames();
                    break;
                case Exchanges.NYSE:
                    data = m_ExchangeHandler.GetNyseNames();
                    break;
                case Exchanges.Nasdaq:
                    data = m_ExchangeHandler.GetNasdaqNames();
                    break;
                case Exchanges.XTSE:
                    data = m_ExchangeHandler.GetTSXNames();
                    break;
                case Exchanges.XTSX:
                    data = m_ExchangeHandler.GetVentureNames();
                    break;
            }            

            if (data != null)
                return true;

            data = new Dictionary<string, string>();
            return false;
        }

        public enum Exchanges
        {
            Amex,
            Nasdaq,
            NYSE,
            Forex,
            /// <summary>
            /// US Options
            /// </summary>
            Opra,
            /// <summary>
            /// Italian Stock Exchange
            /// </summary>
            Milan,
            /// <summary>
            /// London Stock Exchange
            /// </summary>
            LSE,
            /// <summary>
            /// TSX - Toronto Stock Exchange
            /// </summary>
            XTSE,
            /// <summary>
            /// TSX Venture
            /// </summary>
            XTSX
        }


    }
}
